﻿using SmartDevice.Models;
using SmartDevice.Pages.Settings;
using Stylet;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows;

namespace SmartDevice.Pages
{
    public class MainShellViewModel : Screen
    {
        private readonly IWindowManager _windowManager;
        private readonly IViewFactory _viewFactory;
        private readonly IEventAggregator _events;

        public MainShellViewModel(IWindowManager windowManager, IViewFactory viewFactory, IEventAggregator events)
        {
            _windowManager = windowManager;
            _viewFactory = viewFactory;
            _events = events;
        }

        protected override void OnViewLoaded()
        {
            base.OnViewLoaded();

            //发布事件
            Task.Run(() =>
            {
                while (true)
                {
                    _events.Publish(new TimeEvent
                    {
                        Args = new TimeEventArgs
                        {
                            Message = $"Current Time:{DateTime.Now.ToLongTimeString()}"
                        }
                    });
                    Thread.Sleep(1000);
                }
            });
        }

        #region 基本绑定

        public int ProgressValue { get; set; }
        public bool IsProgressShow { get; set; } = true;
        public Visibility ProgressVisibility => IsProgressShow ? Visibility.Visible : Visibility.Collapsed;

        #endregion

        #region 命令

        public string InputString { get; set; }
        public string OutputString { get; set; } = "hehe";
        public void ShowString()
        {
            OutputString = $"Your string is : {InputString}";
        }
        public bool CanShowString => !string.IsNullOrEmpty(InputString);

        #endregion

        #region 绑定列表

        public BindableCollection<string> StringList { get; set; } = new BindableCollection<string>();
        public string SelectedString { get; set; }
        private int Count = 1;

        public void AddString()
        {
            StringList.Add($"Item{Count++}");
        }

        public void DeleteString()
        {
            StringList.Remove(SelectedString);
        }

        public bool CanDeleteString => SelectedString != null;

        #endregion



        #region 打开设置界面

        public void OpenSettingPage()
        {
            _windowManager.ShowDialog(_viewFactory.SettingsViewModel());
        }

        #endregion
    }
}
